package club.dmyang.controller;

import club.dmyang.common.enums.RespCodeEnums;
import club.dmyang.common.vo.ResposeVo;
import club.dmyang.entity.ArticleWithBLOBs;
import club.dmyang.entity.Comment;
import club.dmyang.service.ArticleService;
import club.dmyang.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * Created by brss on 2018/7/25.
 */
@Controller
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 点赞
     * Q如何防止恶意点赞
     * @param aid
     * @return
     */
    @RequestMapping("/vote")
    @ResponseBody
    public ResposeVo voteOneArticle(Integer aid, HttpServletRequest request) {

        //获取Sessionid
        String sessionId = request.getSession().getId() + "_vote";
        if (redisTemplate.opsForValue().get(sessionId) != null){
            return new ResposeVo(RespCodeEnums.RESP_CODE_ERROR.getCode(), "点赞失败,3秒内只能点赞一次!");
        }
        //把Sessionid装入redis，并且设置3秒过期
        redisTemplate.opsForValue().set(sessionId,sessionId,3, TimeUnit.SECONDS);

        int i = articleService.voteOneArticle(aid);
        if ( i<1 )
            return new ResposeVo(RespCodeEnums.RESP_CODE_ERROR.getCode(), "点赞失败！");
        return new ResposeVo(RespCodeEnums.RESP_CODE_SUCC.getCode(), "点赞成功！");
    }

    /**
     * 评论一篇文章
     * @param aid
     * @param commentContent
     * @return
     */

    @ResponseBody
    @RequestMapping("/comment")
    public ResposeVo commentOneArticle(HttpServletRequest httpServletRequest,
                                       HttpServletResponse httpServletResponse,
                                       Integer aid,
                                       String commentContent,
                                       String vericode){

        //验证验证码
        String captchaId = (String) httpServletRequest.getSession().getAttribute("vrifyCode");
        String parameter = httpServletRequest.getParameter("vericode");
        System.out.println("Session  vrifyCode "+captchaId+" form vrifyCode "+parameter);

        if (!captchaId.equals(parameter)) {
            return new ResposeVo(RespCodeEnums.RESP_CODE_ERROR.getCode(),"错误的验证码");
        }

        if (commentContent.equals(""))
            return new ResposeVo(RespCodeEnums.RESP_CODE_ERROR.getCode(), "内容不能为空，评论失败！");

        Comment comment = new Comment();
        comment.setArtiId(aid);
        comment.setContent(HtmlUtils.htmlEscape(commentContent));
        comment.setCreateTime(new Date());
        comment.setVotes(0);
        Comment c = commentService.addOneComment(comment);
        if (c==null)
            return new ResposeVo(1, "评论失败！");

        //评论成功，返回刚才评论的对象
        return new ResposeVo(RespCodeEnums.RESP_CODE_SUCC.getCode(),
                "评论成功！",
                RespCodeEnums.NOT_A_LIST.getCode(),c);
    }

}
